package com.example.szh.mvp.presenter;

import com.example.szh.entity.BaseEntity;
import com.example.szh.entity.UserEntity;
import com.example.szh.http.HttpCode;
import com.example.szh.mvp.contract.UpDataUserContract;
import com.example.szh.nextwork.presenter.BasePresenter;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UpDataUserPresenter extends BasePresenter<UpDataUserContract.IUpDataUserModel,UpDataUserContract.IUpDataUserView> {
    @Inject
    public UpDataUserPresenter(UpDataUserContract.IUpDataUserModel iUpDataUserModel, UpDataUserContract.IUpDataUserView iUpDataUserView) {
        super(iUpDataUserModel, iUpDataUserView);
    }

    public void requestUpDataUser(UserEntity userEntity){
//        {
//            "null": true,
//                "sign": "71b969a19ba86d3f19813843608fab88",
//                "user_id": 1,
//                "user_img": "/sdcard/DCIM/Camera/1621989741886.jpg",
//                "user_integral": 1,
//                "user_name": "guoyangwen",
//                "user_phone": 123456789,
//                "user_pwd": "abcdefg",
//                "user_sex": "男",
//        }
        Map<String,Object> map=new HashMap<>();
        map.put("code", HttpCode.UPDATAUSERCODE);
        map.put("user_id",userEntity.getUser_id());
        map.put("user_img",userEntity.getUser_img());
        map.put("user_integral",userEntity.getUser_integral());
        map.put("user_name",userEntity.getUser_name());
        map.put("user_phone",userEntity.getUser_phone());
        map.put("user_pwd",userEntity.getUser_pwd());
        map.put("user_sex",userEntity.getUser_sex());
        m.request(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void RequestUpload(String path){
        Map<String, Object> map = new HashMap<>();
        File file = new File(path);
        RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.addFormDataPart("file","szh"+System.currentTimeMillis()+".jpg",body);
        List<MultipartBody.Part> list = builder.build().parts();
        map.put("list",list);
        map.put("code", HttpCode.UPLOADIMGCODE);
        m.request(map).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }

    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
        v.refresh(baseEntity);
    }
}
